package com.yucc.girlimage.io;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.yucc.girlimage.io.upyun.FolderItem;
import com.yucc.girlimage.io.upyun.UpYun;
import com.yucc.girlimage.io.upyun.UpYunException;

/**
 * An {@link AsyncTaskLoader} that loads activities from the public Google+
 * stream for a given search query. The loader maintains a page state with
 * the Google+ API and thus supports pagination.
 */
public class StreamLoader extends AsyncTaskLoader<ArrayList<FolderItem>> {
	ArrayList<FolderItem> mFolderList;    
	private boolean mIsLoading;     //indicate the loading status
	private boolean mHasError;    //是否有错
	private String mDirectory;   //请求的目录地址
	private int mNextPage;     //下一个页面的页数
	private boolean isCategoryRequest ; //indicate 是否是分类页面出发之后的页面发出的请求
	private boolean mIsLocal ;	  //指示是否是本地内容
	private String mFolderName ;     //需要显示的Toast内容
	private boolean mShowToast ;    //indicate 是否需要在UI现实Toast
	private boolean mIsLoadMore ;  //indicate 加载更多模式还是正常模式

	public StreamLoader(Context context, String directory) {
		super(context);
		init(directory);
	}

	private void init(String directory) {
		mDirectory = directory;
		mHasError = false;
		mIsLoading = true;
		mFolderList = null;
		mNextPage = 1;
	}

	@Override
	public ArrayList<FolderItem> loadInBackground() {
		mIsLoading = true;
		if(mIsLocal){
			if (mFolderList == null || mIsLoadMore) {
				Db4oHelper helper = new Db4oHelper(getContext());
				try{
					return helper.getFavFolderItems() ;
				}finally{
					helper.close() ;
				}
			}
		}else{
			try {
				mHasError = false;
				if(mFolderList == null && mShowToast){
					mFolderName = UpYun.getFileAccessInstance().readFile(mDirectory+".txt") ;
					if(mFolderName == null) mFolderName = "美女" ;
				}
				
				if (mFolderList == null || mIsLoadMore) {
					ArrayList<FolderItem> list = UpYun.getImageAcessInstance().readDir(
							mDirectory + "/" + mNextPage);
					return list;
				}
			} catch (UpYunException e) {
				e.printStackTrace();
				mHasError = true;
				mNextPage = -1;
			}
		}
		return null;
	}
	
	public void loadMore(){
		mIsLoadMore = true ;
		mNextPage++;
	}
	
	public StreamLoader setCategoryRequest(boolean isCategoryRequest) {
		this.isCategoryRequest = isCategoryRequest;
		return this ;
	}
	
	public String getFolderName(){
		return this.mFolderName  ;
	}

	public boolean isShowToast() {
		return mShowToast;
	}

	public StreamLoader setShowToast(boolean mShowToast) {
		this.mShowToast = mShowToast;
		return this ;
	}

	public StreamLoader setIsLocal(boolean isLocal){
		this.mIsLocal = isLocal ;
		return this ;
	}
	
	private final Comparator<FolderItem> mComparator = new Comparator<FolderItem>() {

		@Override
		public int compare(FolderItem lhs, FolderItem rhs) {
			final int lPoP = lhs.name.indexOf(".") ;
			final int rPoP = rhs.name.indexOf(".") ;
			if(Integer.valueOf(lhs.name.substring(0, lPoP)) > Integer.valueOf(rhs.name.substring(0, rPoP)) )	return 1 ;
			else return -1 ;
		}
	};

	@Override
	public void deliverResult(ArrayList<FolderItem> list) {
		mIsLoading = false;
		if (list != null) {
			if (mFolderList == null) {
				if(isCategoryRequest) Collections.sort(list, mComparator) ;
				mFolderList = list;
			} else if(mIsLoadMore){
				mFolderList.addAll(list);
			}
		}
		mIsLoadMore = false ;
		if (isStarted()) {
			// Need to return new ArrayList for some reason or
			// onLoadFinished() is not called
			super.deliverResult(mFolderList == null ? null
					: new ArrayList<FolderItem>(mFolderList));
		}
	}

	@Override
	protected void onStartLoading() {
		super.forceLoad();
	}

	@Override
	public boolean cancelLoad() {
		return super.cancelLoad();
	}

	@Override
	protected void onStopLoading() {
		super.onStopLoading();
		mIsLoading = false;
		cancelLoad();
	}

	@Override
	protected void onReset() {
		super.onReset();
		onStopLoading();
		mFolderList = null;
	}

	public boolean isLoading() {
		return mIsLoading;
	}

	public boolean hasMoreResults() {
		return mNextPage != -1;
	}

	public boolean hasError() {
		return mHasError;
	}

	public void refresh() {
		reset();
		startLoading();
	}
}
